﻿using System;
using System.Collections.Generic;
using System.Text;
using NSoup.Select;
using System.Collections.Specialized;
using System.Collections;

namespace Com.Toook.AutoDownloadHtml.DownloadHtml.ParseObj
{
    /// <summary>
    /// 获取分页的分类中的详细页面URL
    /// </summary>
    public class GetLinksInPagedKind
    {
        public GetLinksInPagedKind()
        {
        }

        private void GetAllPage()
        {
            string url = null;
            for (int i = 0; i < maxPage; i++)
            {
                url = GetPageUrl(i);
            }
        }
        private void GetXml(string xmlPath)
        {
            string path = xmlPath; //Server.MapPath("~\\App_Data\\StepNodes.xml");
            ParseXmlToStep parseStep = new ParseXmlToStep(path);
            List<Rule> ruleList = parseStep.GetRuleListByKindId("xddyDZ");//FinanceSohu,SportSohu

            int maxPage = 10;
            for (int n = 1; n < maxPage; n++)
            {
                string url = GetPageUrl(n);        //    parseStep.KindSourceUrl;//配置文件中分类页面的原始url

                Step initStep = parseStep.InitStep;//根元素解析步骤
                int minTitleLength = parseStep.MinLinkTextLength;

                //获取分类中的详细页面URL
                GetLinksInKindPage html = new GetLinksInKindPage();
                Elements initElements = html.GetInitRootElements(url, initStep);
                List<LeafResult> leafResultList = html.GetByRuleList(ruleList, initElements, minTitleLength);
                //StringDictionary sd = html.GetByRuleList(ruleList, initElements, minTitleLength);

                //StringBuilder sb = new StringBuilder();
                //foreach (DictionaryEntry de in sd)
                //{
                //    sb.Append("url=" + de.Key.ToString() + ", text=" + de.Value + "<br />");
                //}
            }
            //showText.Text = sb.ToString();
        }
        /// <summary>
        /// 获取第N页url
        /// </summary>
        /// <param name="n"></param>
        public string GetPageUrl(int n)
        {
            string pageN = "";
            switch (rule)
            {
                case "num":
                    pageN = baseUrl + startPart + n.ToString() + endPart;
                    break;
                case "FixedNum":
                    pageN = baseUrl + startPart + n.ToString(fixedNum) + endPart;
                    break;
                default :
                    break;
            }
            return "";
        }

        private string firstUrl;
        private string baseUrl="/list/";
        private string startPart="index_";
        private string endPart=".html";
        private string fixedNum = "D4";
        private int maxPage;//最大页数
        private string rule;
    }
}
